/**
 * Theme functions file
 *
 * Contains handlers for navigation, accessibility, header sizing
 * footer widgets and Featured Content slider
 *
 */
( function( $ ) {
    $(document).ready(function(){
        $('#add-cart-button').click(function(){
            var sids = getCookie('sids');

            var id = $(this).attr('ref');
            if (isNaN(id)) {
                return;
            }

            var aids = [];
            if (sids == '') {
                aids.push(id);
                setCookie('sids',aids.toString(),7);
            } else {
                aids = sids.split(',');
                for (var i = 0 ; i< aids.length ; i++) {
                    if (aids[i] == id){
                        return;
                    }
                }
                aids.push(id);
                setCookie('sids',aids.toString(),7);
            }
            alert(getCookie('sids'));
        });


        // We'll pass this variable to the PHP function example_ajax_request
        var fruit = 'Banana';
        // This does the ajax request
            $.ajax({
                url: ajaxurl,
                data: {
                    'action':'cart_ajax_request',
                    'fruit' : fruit
                },
                success:function(data) {
                    // This outputs the result of the ajax request
                    console.log(data);
                },
                error: function(errorThrown){
                    console.log(errorThrown);
                }
            });
    });
} )( jQuery );



function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);
        }
    }
}

